package com.yunxie.novelservice.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yunxie.novelservice.application.convert.NovelDetailsDTOConverter;
import com.yunxie.novelservice.application.convert.NovelsDTOConverter;
import com.yunxie.novelservice.application.dto.NovelDetailsDTO;
import com.yunxie.novelservice.application.dto.NovelsDTO;
import com.yunxie.novelservice.common.entity.PageResult;
import com.yunxie.novelservice.common.entity.Result;
import com.yunxie.novelservice.domain.convert.NovelsBoConverter;
import com.yunxie.novelservice.domain.entity.NovelDetailsBo;
import com.yunxie.novelservice.domain.entity.NovelsBo;
import com.yunxie.novelservice.domain.service.NovelDetailsDomainService;
import com.yunxie.novelservice.domain.service.NovelsDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小说详情模块
 */
@RestController
@RequestMapping("/novel/novels")
@Slf4j
public class NovelsController {
    @Resource
    private NovelsDomainService novelsDomainService;

    /**
     * 查询小说接口
     * @param novelsDTO
     * @return
     */
    @PostMapping("queryNovels")
    public Result<NovelDetailsDTO> queryNovel(@RequestBody NovelsDTO novelsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.queryNovels.dto:{}", JSON.toJSONString(novelsDTO));
            }
            Preconditions.checkNotNull(novelsDTO.getNovelsId(),"小说id不能为空");
            NovelsBo queryNovels = novelsDomainService.queryNovels(novelsDTO.getNovelsId());
            NovelsDTO result = NovelsDTOConverter.INSTANCE.convertBoToDTO(queryNovels);

            return  Result.ok(result);
        }catch (Exception e){
            log.error("NovelDetailsController.queryNovels.error:{}",e.getMessage(),e);
            return Result.fail("获取小说失败");
        }
    }
    /**
     * 查询小说集合分页接口
     * @param novelsDTO
     * @return
     */
    @PostMapping("queryNovelsList")
    public Result<PageResult<NovelsDTO>> queryNovelsList(@RequestBody NovelsDTO novelsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.queryNovelsList.dto:{}", JSON.toJSONString(novelsDTO));
            }
            NovelsBo novelsBo = NovelsDTOConverter.INSTANCE.convertDTOToBo(novelsDTO);
            PageResult<NovelsBo> queryNovelsList = novelsDomainService.queryNovelsList(novelsBo);
            return  Result.ok(queryNovelsList);
        }catch (Exception e){
            log.error("NovelDetailsController.queryNovelsList.error:{}",e.getMessage(),e);
            return Result.fail("获取小说集合分页失败");
        }
    }

    /**
     * 添加小说接口
     * @param novelsDTO
     * @return
     */
    @PostMapping("addNovels")
    public Result<NovelDetailsDTO> addNovels(@RequestBody NovelsDTO novelsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.addNovels.dto:{}", JSON.toJSONString(novelsDTO));
            }

            Preconditions.checkNotNull(novelsDTO.getName(),"小说名字不能为空");
            Preconditions.checkNotNull(novelsDTO.getGenreId(),"小说类型不能为空");
            NovelsBo novelsBo = NovelsDTOConverter.INSTANCE.convertDTOToBo(novelsDTO);
            novelsDomainService.addNovels(novelsBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.addNovels.error:{}",e.getMessage(),e);
            return Result.fail("添加小说失败");
        }
    }
    /**
     * 删除小说接口
     * @param novelsDTO
     * @return
     */
    @PostMapping("deleteNovels")
    public Result<NovelDetailsDTO> deleteNovels(@RequestBody NovelsDTO novelsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.deleteNovels.dto:{}", JSON.toJSONString(novelsDTO));
            }
            Preconditions.checkNotNull(novelsDTO.getNovelsId(),"小说id不能为空");
            novelsDomainService.deleteNovels(novelsDTO.getNovelsId());
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.deleteNovels.error:{}",e.getMessage(),e);
            return Result.fail("删除小说失败");
        }
    }
    /**
     * 修改小说接口
     * @param novelsDTO
     * @return
     */
    @PostMapping("updateNovels")
    public Result<NovelDetailsDTO> updateNovels(@RequestBody NovelsDTO novelsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.updateNovels.dto:{}", JSON.toJSONString(novelsDTO));
            }
            Preconditions.checkNotNull(novelsDTO.getNovelsId(),"小说id不能为空");
            Preconditions.checkNotNull(novelsDTO.getName(),"小说名字不能为空");
            Preconditions.checkNotNull(novelsDTO.getGenreId(),"小说类型不能为空");
            NovelsBo novelsBo = NovelsDTOConverter.INSTANCE.convertDTOToBo(novelsDTO);
            novelsDomainService.updateNovels(novelsBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.updateNovels.error:{}",e.getMessage(),e);
            return Result.fail("修改小说失败");
        }
    }

}
